package com.xindaoapp.happypet.ui;

import java.util.Map;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.xindaoapp.happypet.R;
import com.xindaoapp.happypet.activity.PrivateLetterActivity;
import com.xindaoapp.happypet.bean.User;
import com.xindaoapp.happypet.protocol.MoccaApi;
import com.xindaoapp.happypet.utils.CommonParameter.UserState;
import com.xindaoapp.happypet.utils.Constants;
import com.xindaoapp.happypet.utils.IRequest;
import com.xindaoapp.happypet.viewpager.SwitchTabViewPager;
import com.xindaoapp.happypet.viewpager.ViewPagerAdapter;
import com.xindaoapp.happypet.viewpager.ViewPagerWindow;
import com.xindaoapp.happypet.viewpager.windows.UserMyAttentionWindow;
import com.xindaoapp.happypet.viewpager.windows.UserMyLoveWindow;
import com.xindaoapp.happypet.viewpager.windows.UserMyPostWindow;

/**
 * 用户中心
 * 
 * @author Stone
 * 
 */
public class MyUserCenterActivity extends BaseActivity implements OnClickListener {

    private SwitchTabViewPager vViewPager;
    private ImageView vAttention;

    private User mFriendUserInfo;
    private String mFriendUid;
    private String mFriendUserName;
    private LoginSuccessReceiver mLoginSuccessReceiver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }


    @Override
    protected int getContentViewResId() {
        // TODO Auto-generated method stub
        return R.layout.activity_my_user_center;
    }

    @Override
    protected void initViews() {
        super.initViews();
        mLoginSuccessReceiver = new LoginSuccessReceiver();
        registerReceiver(mLoginSuccessReceiver, new IntentFilter(
                Constants.ACTION_LOGIN_STATE_CHANGED));
        mFriendUid = getIntent().getStringExtra(Constants.KEY_UID);
        mFriendUserName = getIntent().getStringExtra(Constants.KEY_USERNAME);
        // initTopBg(view);
        vAttention = (ImageView) findViewById(R.id.attention);
        ImageLoader.getInstance().displayImage(getIntent().getStringExtra(Constants.KEY_USERFACE),
                ((ImageView) findViewById(R.id.user_icon)));
        ((TextView) findViewById(R.id.user_name)).setText(mFriendUserName);
        vViewPager = (SwitchTabViewPager) findViewById(R.id.viewpager);
        // vViewPager = (SwitchTabViewPager) findViewById(R.id.viewpager);
        vViewPager.addTabs(new View[] {findViewById(R.id.tab_invitation),
                findViewById(R.id.tab_attention), findViewById(R.id.tab_fans)}, 0, true, false);
        ViewPagerWindow[] windows =
                new ViewPagerWindow[] {
                        new UserMyLoveWindow(MyUserCenterActivity.this, getIntent().getStringExtra(
                                Constants.KEY_UID)),
                        new UserMyPostWindow(MyUserCenterActivity.this, getIntent().getStringExtra(
                                Constants.KEY_UID)),
                        new UserMyAttentionWindow(MyUserCenterActivity.this, getIntent()
                                .getStringExtra(Constants.KEY_UID))};
        vViewPager.addPagerWindows(windows);
        vViewPager.setAdapter(new ViewPagerAdapter(windows));
        if (getIntent() != null) {
            vViewPager.setCurrentItem(getIntent().getIntExtra(Constants.KEY_SKIP_TAB_INDEX, 0));
        }
    }

    @Override
    protected void initEvents() {
        super.initEvents();
        vAttention.setOnClickListener(this);
        (findViewById(R.id.back)).setOnClickListener(this);
        (findViewById(R.id.sixin)).setOnClickListener(this);
    }

    @Override
    protected void onLoadDatas() {
        super.onLoadDatas();
        vAttention.setEnabled(false);
        getMoccaApi().getUserInfoByUid(mFriendUid, new IRequest<Map<String, Object>>() {

            @Override
            public void request(Map<String, Object> t) {
                if (t != null && t.containsKey(MoccaApi.RESPONSE_USERINFO)) {
                    mFriendUserInfo = (User) t.get(MoccaApi.RESPONSE_USERINFO);
                    ((TextView) findViewById(R.id.fans_count)).setText(String.format("%s粉丝",
                            mFriendUserInfo.fanscount));
                    vAttention.setEnabled(true);
                    handleAttentionViewState(mFriendUserInfo.follow);
                }
            }
        });
    }

    @Override
    protected OnClickListener getRightViewClickListener() {
        // TODO Auto-generated method stub
        return super.getRightViewClickListener();
    }

    @Override
    protected int getTopBarRightImageViewRes() {
        // TODO Auto-generated method stub
        return super.getTopBarRightImageViewRes();
    }

    @Override
    protected String getTopBarTitleStrRes() {
        // TODO Auto-generated method stub
        return super.getTopBarTitleStrRes();
    }

    @Override
    protected OnClickListener getLeftViewClickListener() {
        return new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                finish();
            }
        };
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        unregisterReceiver(mLoginSuccessReceiver);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.attention:
                if (UserState.isLogged(this)) {
                    handleAttention();
                }
                break;
            case R.id.back:
                finish();
                break;
            case R.id.sixin:
                Intent i = new Intent(this, PrivateLetterActivity.class);
                i.putExtra(Constants.KEY_TOUID, mFriendUid);
                i.putExtra(Constants.KEY_USERNAME, mFriendUserName);
                i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(i);
                break;
            default:
                break;
        }
    }

    private void handleAttentionViewState(int follow) {
        if (mFriendUserInfo.follow == 0) {
            vAttention.setImageResource(R.drawable.follow);
        } else if (mFriendUserInfo.follow == 1) {
            vAttention.setImageResource(R.drawable.followed);
        }
    }

    private void handleAttention() {
        if (null == mFriendUserInfo) {
            return;
        }
        handleAttentionViewState(mFriendUserInfo.follow == 0 ? 1 : 0);
        vAttention.setEnabled(false);
        if (mFriendUserInfo.follow == 0) {
            getMoccaApi().addFollowAdd(mFriendUid, mFriendUserInfo.username,
                    new IRequest<Boolean>() {

                        @Override
                        public void request(Boolean t) {
                            mFriendUserInfo.follow = t ? 1 : 0;
                            vAttention.setEnabled(true);
                            handleAttentionViewState(mFriendUserInfo.follow);
                        }
                    });
        } else if (mFriendUserInfo.follow == 1) {
            getMoccaApi().deleteFollowAdd(mFriendUid, new IRequest<Boolean>() {

                @Override
                public void request(Boolean t) {
                    mFriendUserInfo.follow = t ? 0 : 1;
                    vAttention.setEnabled(true);
                    handleAttentionViewState(mFriendUserInfo.follow);
                }
            });
        }
    }

    class LoginSuccessReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            onLoadDatas();
        }

    }


}
